package com.crm.system.service.impl.opportunityManager;

import com.crm.common.model.DhPageInfo;
import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.opportunityManager.OpportunityGift;
import com.crm.model.entity.opportunityManager.OpportunityProduct;
import com.crm.model.entity.sys.UserAccount;
import com.crm.service.opportunityManager.OpportunityGiftDbService;
import com.crm.system.service.opportunityManager.OpportunityGiftService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * bj
 * 商机赠品
 */
@Service
public class OpportunityGiftServiceImpl implements OpportunityGiftService {

    @Autowired
    private OpportunityGiftDbService opportunityGiftDbService;

    /**
     * 商机-新增商机-查询商机下的赠品
     * @param pageIndex 页码
     * @param pageSize 分页大小
     * @param sortName 排序列名字
     * @param sortOrder 排序规则
     * @param opportunityId 商机id
     * @return
     */
    @Override
    public DhPageInfo<OpportunityGift> opportunityGiftList(Integer pageIndex, Integer pageSize, String sortName, String sortOrder, String opportunityId) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }

        List<OpportunityGift> list = opportunityGiftDbService.opportunityGiftList(opportunityId,"create_tm desc");
        PageInfo<OpportunityGift> pageInfo = new PageInfo<OpportunityGift>(list);
        return new DhPageInfo<OpportunityGift>(pageInfo, list);

    }

    /**
     * 商机管理-新增商机赠品
     * @param opportunityGift 商机赠品实体
     * @return
     */
    @Override
    public String addOpportunityGift(OpportunityGift opportunityGift) {

        List<OpportunityGift> list = opportunityGiftDbService.opportunityCodeAndOppIdList(opportunityGift.getGiftCode(),opportunityGift.getOpportunityId());
        if(list.size() == 0){
            UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
            String opportunityGiftId = CommonUtils.getGUID();
            opportunityGift.setOpportunityGiftId(opportunityGiftId);
            opportunityGift.setCreatePeopleId(userAccount.getAccountId());
            opportunityGift.setCreateTm(new Date());
            opportunityGift.setUpdatePeopleId(userAccount.getAccountId());
            opportunityGift.setUpdateTm(new Date());
            opportunityGift.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
            Boolean result = opportunityGiftDbService.addOpportunityGift(opportunityGift);
            return "success";
        }else{
            return "EX0001";
        }
    }

    /**
     * 商机-商机详情-修改商机赠品回显
     * @param opportunityGiftId 商机赠品id
     * @return
     */
    @Override
    public OpportunityGift getOpportunityGiftById(String opportunityGiftId) {
        return opportunityGiftDbService.getOpportunityGiftById(opportunityGiftId);
    }

    /**
     * 商机管理-修改商机赠品
     * @param opportunityGift 商机赠品实体
     * @return
     */
    @Override
    public Boolean updateOpportunityGift(OpportunityGift opportunityGift) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        opportunityGift.setUpdatePeopleId(userAccount.getAccountId());
        opportunityGift.setUpdateTm(new Date());
        Boolean result = opportunityGiftDbService.updateOpportunityGift(opportunityGift);
        return result;
    }

    /**
     * 商机管理-删除商机赠品
     * @param opportunityGift 商机赠品实体
     * @return
     */
    @Override
    public Boolean deleteOpportunityGift(OpportunityGift opportunityGift) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        opportunityGift.setUpdatePeopleId(userAccount.getAccountId());
        opportunityGift.setUpdateTm(new Date());
        opportunityGift.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.DELETE_CODE.getState())));
        return opportunityGiftDbService.updateOpportunityGift(opportunityGift);
    }
}
